<template>
  <div class="answer-input">
    <!-- 单选题 -->
    <div v-if="question.type === 'radio'" class="radio-group">
      <el-radio-group v-model="modelValue">
        <el-radio
            v-for="(option, index) in question.options"
            :key="index"
            :label="option"
            class="option-item"
            border
        >
          {{ option }}
        </el-radio>
      </el-radio-group>
    </div>

    <!-- 多选题 -->
    <div v-else-if="question.type === 'checkbox'" class="checkbox-group">
      <el-checkbox-group v-model="modelValue">
        <el-checkbox
            v-for="(option, index) in question.options"
            :key="index"
            :label="option"
            class="option-item"
            border
        >
          {{ option }}
        </el-checkbox>
      </el-checkbox-group>
    </div>

    <!-- 文本题 -->
    <div v-else-if="question.type === 'text'" class="text-input">
      <el-input
          v-model="modelValue"
          type="textarea"
          :rows="4"
          :maxlength="500"
          show-word-limit
          placeholder="请输入您的回答"
          resize="none"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

const props = defineProps({
  question: {
    type: Object,
    required: true
  },
  modelValue: {
    type: [String, Array],
    default: ''
  }
})

const emit = defineEmits(['update:modelValue'])

const modelValue = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})
</script>

<style scoped>
.answer-input {
  margin-top: 10px;
}

.radio-group,
.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.option-item {
  margin: 0;
  padding: 10px 15px;
  border-radius: 4px;
  transition: all 0.3s;
}

.option-item:hover {
  background-color: #f5f7fa;
}

.text-input {
  margin-top: 8px;
}
</style>